<template>
    <div class="login-container">
        <div class="login-content">
            <div class="brand">
                <img class="logo" src="../assets/3.png" alt="logo">
                <h2 class="slogan">专业的公路车自组平台</h2>
            </div>

            <div class="login-form">
                <div class="input-group">
                    <div class="input-field">
                        <el-input
                            v-model="username"
                            placeholder="请输入用户名/手机号"
                            :prefix-icon="User"
                        />
                    </div>

                    <div class="input-field">
                        <el-input
                            v-model="password"
                            type="password"
                            placeholder="请输入密码"
                            show-password
                            :prefix-icon="Lock"
                        />
                    </div>
                </div>

                <div class="options">
                    <el-checkbox v-model="remember">
                        <span class="remember-text">记住我</span>
                    </el-checkbox>
                    <router-link to="/findback" class="forget-link">忘记密码？</router-link>
                </div>

                <el-button 
                    type="primary" 
                    class="login-btn" 
                    @click="login"
                    :loading="isLoading"
                >
                    登录
                </el-button>

                <div class="register-prompt">
                    还没有账号？
                    <router-link to="/register" class="register-link">立即注册</router-link>
                </div>

                <div class="divider">
                    <span>其他登录方式</span>
                </div>

                <div class="social-login">
                    <button class="social-btn weixin" @click="socialLogin('weixin')">
                        <i class="iconfont icon-weixin"></i>
                    </button>
                    <button class="social-btn qq" @click="socialLogin('qq')">
                        <i class="iconfont icon-QQ"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { userLogin } from '@/api/index.js'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import md5 from 'md5'
import { User, Lock } from '@element-plus/icons-vue'

const username = ref('')
const password = ref('')
const remember = ref(false)
const router = useRouter()
const isLoading = ref(false)

const login = async() => {
    if (username.value.trim() === '' || password.value.trim() === '') {
        ElMessage.error('账号或密码不能为空')
        return
    }

    try {
        isLoading.value = true
        const res = await userLogin({
            username: username.value.trim(), 
            password: md5(password.value)
        })

        if (remember.value) {
            localStorage.setItem('token', res.token)
        } else {
            localStorage.removeItem('token')
            sessionStorage.setItem('token', res.token)
        }

        localStorage.setItem('userInfo', JSON.stringify(res.data))
        ElMessage.success('登录成功')
        router.back()
    } catch (error) {
        ElMessage.error(error.message || '登录失败')
    } finally {
        isLoading.value = false
    }
}

const socialLogin = (type) => {
    ElMessage.info(`${type}登录功能开发中...`)
}
</script>

<style lang="css" scoped>
.login-container {
    min-height: 100vh;
    width: 100vw;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
}

.login-content {
    padding: 40px 20px;
    height: 100vh;
    overflow-y: auto;
    box-sizing: border-box;
}

.brand {
    text-align: center;
    padding-top: 40px;
    margin-bottom: 48px;
}

.logo {
    width: 80px;
    height: 80px;
    object-fit: contain;
    margin-bottom: 16px;
    animation: bounce 1s ease-out;
}

.slogan {
    font-size: 18px;
    color: #1a1a1a;
    font-weight: 500;
    margin: 0;
}

.login-form {
    padding: 0 20px;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 24px;
}

.input-field :deep(.el-input__wrapper) {
    padding: 12px 16px;
    border-radius: 25px;
    background: rgba(255, 255, 255, 0.9);
    border: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.input-field :deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding: 0 4px;
}

.remember-text {
    color: #666;
    font-size: 14px;
}

.forget-link {
    color: #409eff;
    text-decoration: none;
    font-size: 14px;
}

.login-btn {
    width: 100%;
    height: 44px;
    font-size: 16px;
    border-radius: 25px;
    background: #409eff;
    border: none;
    margin-bottom: 20px;
}

.login-btn:active {
    transform: scale(0.98);
}

.register-prompt {
    text-align: center;
    margin: 24px 0;
    color: #666;
    font-size: 14px;
}

.register-link {
    color: #409eff;
    text-decoration: none;
    font-weight: 500;
}

.divider {
    text-align: center;
    position: relative;
    margin: 32px 0;
}

.divider::before,
.divider::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 35%;
    height: 1px;
    background: rgba(0, 0, 0, 0.1);
}

.divider::before {
    left: 0;
}

.divider::after {
    right: 0;
}

.divider span {
    background: transparent;
    padding: 0 12px;
    color: #666;
    font-size: 14px;
}

.social-login {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin-top: 24px;
}

.social-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.social-btn:active {
    transform: scale(0.95);
}

.weixin {
    background: #07C160;
    color: white;
}

.qq {
    background: #12B7F5;
    color: white;
}

.social-btn i {
    font-size: 24px;
}

@keyframes bounce {
    0% { transform: scale(0.3); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* 适配 iPhone X 及以上机型的底部安全区域 */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .login-content {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
}

/* 禁用页面滚动回弹效果 */
:deep(*) {
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}
</style>